package com.atguigu01.collection;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class CircleSquaresDemo extends JPanel {
    private static final int RADIUS = 300; // 设置圆的半径
    private static final int DIVISIONS = 12; // 设置等分数量
    private static final int SQUARE_SIZE = 20; // 设置正方形的边长
    private static final double SQUARE_INTERVAL = SQUARE_SIZE * 1.2; // 计算正方形之间的间隔

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g; // 将Graphics对象转换为Graphics2D对象
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // 开启抗锯齿
        int centerX = getWidth() / 2; // 获取面板中心横坐标
        int centerY = getHeight() / 2; // 获取面板中心纵坐标
        int diameter = 2 * RADIUS; // 计算圆的直径
        int x = centerX - RADIUS; // 计算圆的左上角横坐标
        int y = centerY - RADIUS; // 计算圆的左上角纵坐标
        g2d.drawOval(x, y, diameter, diameter); // 绘制圆
        for (int i = 0; i < DIVISIONS; i++) {
            double angle1 = 2 * Math.PI * i / DIVISIONS; // 计算扇形的起始角度
            double angle2 = 2 * Math.PI * (i + 1) / DIVISIONS; // 计算扇形的终止角度
            int x1 = centerX + (int) (RADIUS * Math.cos(angle1)); // 计算扇形起始点横坐标
            int y1 = centerY + (int) (RADIUS * Math.sin(angle1)); // 计算扇形起始点纵坐标
            int x2 = centerX + (int) (RADIUS * Math.cos(angle2)); // 计算扇形终止点横坐标
            int y2 = centerY + (int) (RADIUS * Math.sin(angle2)); // 计算扇形终止点纵坐标
            g2d.drawLine(centerX, centerY, x1, y1); // 绘制分割线
        }

        int squareCount = 1000; // 设置正方形的数量
        List<Integer> squareSizes = new ArrayList<>(); // 存储每个同心圆的正方形大小
        int radius = SQUARE_SIZE; // 计算第一个同心圆的半径
        while (squareSizes.size() < squareCount) {
            int squareSize = (int) (SQUARE_SIZE + (SQUARE_SIZE * 0.2 * (squareSizes.size() / DIVISIONS))); // 计算同心圆上的正方形大小
            squareSizes.add(squareSize);
            radius += squareSize + SQUARE_INTERVAL; // 计算下一个同心圆的半径
        }
        for (int i = 0; i < squareSizes.size(); i++) {
            int circleRadius = radius - (squareSizes.get(i) / 2); // 计算同心圆的半径
            int circleX = centerX - circleRadius; // 计算同心圆的左上角横坐标
            int circleY = centerY - circleRadius; // 计算同心圆的左上角纵坐标
            int squareCountInCircle = (int) (2 * Math.PI * circleRadius / (SQUARE_SIZE + SQUARE_INTERVAL)); // 计算同心圆上正方形的数量
            double[] angles = new double[squareCountInCircle]; // 存储同心圆上每个正方形的角度
            for (int j = 0; j < squareCountInCircle; j++) {
                angles[j] = 2 * Math.PI * j / squareCountInCircle; // 计算同心圆上每个正方形的角度
            }
            for (int j = 0; j < squareCountInCircle; j++) {
                int x1 = circleX + (int) ((circleRadius - SQUARE_SIZE / 2) * Math.cos(angles[j])); // 计算正方形的横坐标
                int y1 = circleY + (int) ((circleRadius - SQUARE_SIZE / 2) * Math.sin(angles[j])); // 计算正方形的纵坐标
                boolean isOverlap = false; // 判断当前正方形是否与之前的正方形重叠
                for (int k = 0; k < i; k++) {
                    int overlapRadius = radius - (squareSizes.get(k) / 2) - (SQUARE_SIZE / 2) - (int) (SQUARE_INTERVAL / 2); // 计算与前一同心圆上正方形的重叠半径
                    int overlapX = centerX - overlapRadius; // 计算重叠正方形所在同心圆的左上角横坐标
                    int overlapY = centerY - overlapRadius; // 计算重叠正方形所在同心圆的左上角纵坐标
                    for (int l = 0; l < squareSizes.get(k) / SQUARE_SIZE; l++) {
                        double overlapAngle = 2 * Math.PI * l / (squareSizes.get(k) / SQUARE_SIZE); // 计算重叠正方形所在同心圆上每个正方形的角度
                        int overlapX1 = overlapX + (int) ((overlapRadius - SQUARE_SIZE / 2) * Math.cos(overlapAngle)); // 计算重叠正方形的横坐标
                        int overlapY1 = overlapY + (int) ((overlapRadius - SQUARE_SIZE / 2) * Math.sin(overlapAngle)); // 计算重叠正方形的纵坐标
                        if (x1 + SQUARE_SIZE > overlapX1 && x1 < overlapX1 + SQUARE_SIZE && y1 + SQUARE_SIZE > overlapY1 && y1 < overlapY1 + SQUARE_SIZE) {
                            isOverlap = true;
                            break;
                        }
                    }
                    if (isOverlap) {
                        break;
                    }
                }
                if (!isOverlap) {
                    g2d.setColor(Color.BLACK);
                    g2d.drawRect(x1, y1, SQUARE_SIZE, SQUARE_SIZE); // 绘制正方形边框
                    g2d.setColor(Color.WHITE);
                    g2d.fillRect(x1 + 1, y1 + 1, SQUARE_SIZE - 1, SQUARE_SIZE - 1); // 填充正方形
                }
            }
        }
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Circle Squares");
        frame.setSize(500, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        CircleSquaresDemo panel = new CircleSquaresDemo();
        frame.add(panel);
        frame.setVisible(true);
    }
}